<template>
  <div id="detail">
    <detail-nav-bar ref="nav" @titleClick="titleClick" class="detail-nav-bar"></detail-nav-bar>
    <scroll @scroll="contentScroll" ref="scroll" class="detail-content" :probeType="3" :pullUpLoad="true">
      <detail-swiper :top-images="topImages"></detail-swiper>
      <detail-base-info :goods="goods"></detail-base-info>
      <detail-shop-info :shop="shop"></detail-shop-info>
      <detail-goods-info :detail-info="detailInfo" @imageLoad="imageLoad"></detail-goods-info>
      <detail-param-info ref="param" :param-info="paramInfo"></detail-param-info>
      <detail-comment-info ref="comment" :comment-info="commentInfo"></detail-comment-info>
      <goods-list ref="recommend" :goods="recommends"></goods-list>
    </scroll>
      <detail-bottom-bar @addToCart="addToCart"></detail-bottom-bar>
      <back-top @click="backClick" v-show="isShowBackTop" />
  </div>
</template>

<script>
import DetailNavBar from "./childComps/DetailNavBar";
import DetailSwiper from "./childComps/DetailSwiper";
import DetailBaseInfo from "./childComps/DetailBaseInfo";
import DetailShopInfo from "./childComps/DetailShopInfo";
import DetailGoodsInfo from "./childComps/DetailGoodsInfo";
import DetailParamInfo from "./childComps/DetailParamInfo";
import DetailCommentInfo from "./childComps/DetailCommentInfo";
import DetailBottomBar from "./childComps/DetailBottomBar";
import BackTop from "../../components/content/backTop/BackTop";
import GoodsList from "../../components/content/goods/GoodsList";

import { getRecommend,getDetail, Goods, GoodsParam, Shop } from "../../network/detail";
import Scroll from "../../components/common/scroll/Scroll.vue";

export default {
  name: "Detail",
  components: {
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommentInfo,
    DetailBottomBar,
    GoodsList,
    Scroll,
    BackTop,
  },
  data() {
    return {
      iid: null,
      topImages: [],
      goods: {},
      shop: {},
      detailInfo:{},
      paramInfo:{},
      commentInfo:{},
      recommends:[],
      themeTopYs:[],
      currentIndex:0,
      isShowBackTop:false,
    };
  },
  created() {
    //保存传入的iid
    (this.iid = this.$route.params.iid),
      //根据iid请求详情数据
      getDetail(this.iid).then((res) => {
        //1.获取顶部的图片轮播数据
        const data = res.result;
        this.topImages = data.itemInfo.topImages;
        // 获取商品信息
        this.goods = new Goods(
          data.itemInfo,
          data.columns,
          data.shopInfo.services
        );
        // 获取店铺信息
        this.shop = new Shop(data.shopInfo);
        // 保存商品的详情数据
        this.detailInfo = data.detailInfo;
        // 获取参数的信息
        this.paramInfo = new GoodsParam(data.itemParams.info,data.itemParams.rule)
        // 取出评论的信息
        if(data.rate.cRate !== 0){
          this.commentInfo = data.rate.list[0]
        }
        
      });
      // 请求推荐数据
      getRecommend().then( res => {
        this.recommends = res.data.list
      })
  },
  methods: {
    imageLoad() {
      // this.$refs.scroll.bs.refresh()
      this.themeTopYs = []
      this.themeTopYs.push(0);
      this.themeTopYs.push(this.$refs.param.$el.offsetTop-44);
      this.themeTopYs.push(this.$refs.comment.$el.offsetTop-44);
      this.themeTopYs.push(this.$refs.recommend.$el.offsetTop-44);
    },
    titleClick(index) {
      this.$refs.scroll.bs.scrollTo(0,-this.themeTopYs[index],100)
    },
    contentScroll(position) {
      this.isShowBackTop = -position.y > 1000;
      const positionY = -position.y
      let length = this.themeTopYs.length
      for(let i = 0; i < length; i++){
        if (this.currentIndex !== i &&((i < length -1 && positionY > this.themeTopYs[i] && positionY < this.themeTopYs[i+1]) || ( i === length - 1 && positionY > this.themeTopYs[i]))){
          this.currentIndex = i;
          this.$refs.nav.currentIndex = this.currentIndex;
        }
      }
    },
    backClick(){
      this.$refs.scroll.scrollTo(0,0,300);
    },
    addToCart(){
      const product = {}
      product.image = this.topImages[0];
      product.title = this.goods.title;
      product.desc = this.goods.desc;
      product.price = this.goods.oldPrice;
      product.iid = this.iid;

      this.$store.commit('addCart',product)
      this.$store.dispatch('addCart',product).then(res => {
        
      })
    }
  },
};
</script>

<style scoped>
#detail {
    height: 100vh;
}
.detail-content {
    height: calc(100% - 44px - 58px);
}

.detail-nav-bar {
    position: relative;
    z-index: 9;
    background-color: #fff;
}
</style>